﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Box2D.XNA;

namespace AAEngine.Box2D
{
    public class CollisionShape
    {
        private bool _isTrigger = false;
        private float _density = 1;
        private float _friction = 0;
        private float _restitution = 1;

        protected Box2DSpatialComponent _parent = null;

        public float Density
        {
            get { return _density; }
            set
            {
                _density = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        public float Friction
        {
            get { return _friction; }
            set
            {
                _friction = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        public float Restitution
        {
            get { return _restitution; }
            set
            {
                _restitution = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        public bool IsTrigger
        {
            get { return _isTrigger; }
            set
            {
                _isTrigger = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        public FixtureDef CreateFixture(Box2DSpatialComponent parent)
        {
            _parent = parent;

            FixtureDef fixture = DoCreateFixture();
            fixture.friction = Friction;
            fixture.density = Density;
            fixture.restitution = Restitution;
            fixture.isSensor = IsTrigger;
            fixture.userData = parent;

            if (parent.CollisionType != null)
            {
                fixture.filter.categoryBits = (ushort)parent.CollisionType.Bits;
            }

            if (parent.CollidesWithTypes != null)
            {
                fixture.filter.maskBits = (ushort)parent.CollidesWithTypes.Bits;
            }

            return fixture;
        }

        protected virtual FixtureDef DoCreateFixture()
        {
            return new FixtureDef();
        }
    }
}
